import Vuex from 'vuex'
export default function initToast(v) {
	// 挂在store到全局Vue原型上
	v.prototype.$toastStore = new Vuex.Store({
		state: {
			show: false,
			content: '',
			delay: 1500
		},
		mutations: {
			hideToast(state) {
				// 小程序导航条页面控制
				// #ifndef H5
				if (state.hideTabBar) {
					wx.showTabBar();
				}
				// #endif
				state.show = false
			},
			showToast(state, data) {
				if (data !== null && typeof data === 'object') {
					state = Object.assign(state, data)
				} else {
					state.content = data
				}
				state.show = true
				setTimeout(() => {
					state.show = false
				}, state.delay)
			}
		}
	})
	// 注册$showToast到Vue原型上，以方便全局调用
	v.prototype.$toast = (option) => {
		return new Promise((resolve, reject) => {
			// #ifndef H5
			if (option.hideTabBar) {
				wx.hideTabBar();
			}
			// #endif
			v.prototype.$toastStore.commit('showToast', option)
			setTimeout(() => {
				resolve()
			}, option.delay || 1500)

		})

	}
}